<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.pager.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    // call the tablesorter plugin
                    $("table").tablesorter({
                        // sort on the first column and third column, order asc
                        //sortList: [[1,0]],
                        headers: {
                            // assign the secound column (we start counting zero)
                            0: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            1: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            2: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            3: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            4: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            }
                        }
                        ,
                        widthFixed: true, widgets: ['zebra']
                    })
                    .tablesorterPager({container: $("#pager")});
                    ;
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmDrop()
                {
                    var agree=confirm("Are you sure you want to drop selected student?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

            </script>

            <title>TUP Student Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="AMMSelectSection.php"><b>[ Select Section ]</b></a>&nbsp;&nbsp;
                    </div>
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="AMMRecordAttendance.php">[ Add Attendance ]</a>&nbsp;&nbsp;
                        <a href="AMMViewAttendance.php">View Attendance</a>&nbsp;&nbsp;

                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="ProfEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>

                </div>

                <div align="center" style="  float:left; height:auto; width: 100%;">
                    <h2>Record Attendance</h2>

                    <h3>Attendance Information</h3>

                <?php
                if ($_SESSION['codesecsemyear'] == null) {
                    $codesecsemyear = $_POST['codesecsemyear'];
                    $_SESSION['codesecsemyear'] = $codesecsemyear;
                }
                else
                    $codesecsemyear= $_SESSION['codesecsemyear'];

                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';

                mysql_select_db($dbname);
                $querytext = "select * from coursetable where codesecsemyear='" . $codesecsemyear . "'";
                $resulttext = mysql_query($querytext);
                $text = mysql_fetch_array($resulttext);

                $nummeet = $text['num_of_meeting'];


                if (isset($_POST['Add'])) {
                    $statctr = 1;
                    $allstat = "";
                    $studctr = 1;
                    $allstud = "";

                    while ($_POST["stat" . $statctr]) {
                        $check = $statctr + 1;
                        if ($_POST["stat" . $check])
                            $allstat = $allstat . $_POST["stat" . $statctr] . ";";
                        else
                            $allstat=$allstat . $_POST["stat" . $statctr];
                        $statctr++;
                    }
                    //echo $allstat;

                    while ($_POST["stud" . $studctr]) {
                        $checkstid = $studctr + 1;
                        if ($_POST["stud" . $checkstid])
                            $allstud = $allstud . $_POST["stud" . $studctr] . ";";
                        else
                            $allstud=$allstud . $_POST["stud" . $studctr];
                        $studctr++;
                    }
                    //echo "<br>".$allstud;
                    $mm = $_POST['mm'];
                    $dd = $_POST['dd'];
                    $yyyy = $_POST['yyyy'];
                    $dash = '-';
                    $date = $yyyy . $dash . $mm . $dash . $dd;
                    $codesecsemyeardate = $codesecsemyear . $date;
                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    $numquery = "SELECT meeting_num from attendance where codesecsemyear='" . $codesecsemyear . "' order by meeting_num DESC";
                    mysql_select_db($dbname);
                    $num_meet = mysql_fetch_array(mysql_query($numquery));
                    if ($num_meet['meeting_num'] == null)
                        $num = 1;
                    else
                        $num=$num_meet['meeting_num'] + 1;

                    if ($num <= $nummeet) {
                        $query = "INSERT INTO attendance(codesecsemyeardate, codesecsemyear, student, date, meeting_num, status)
                            VALUES('$codesecsemyeardate', '$codesecsemyear', '$allstud', '$date', '$num','$allstat' ) ";

                        $queryvalue = mysql_query($query);
                        if ($queryvalue == 0) {
                            echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                            echo "<h3 class=\"err\">Input failed</h3>";
                        } else if ($queryvalue == 1)
                            echo "<h3 class=\"err\">Attendance has been added</h3>";
                    }
                    else
                        echo "<h3 class=\"err\">Max Number of Meetings reach</h3>";

                    include 'DBLibrary/closeDB.php';
                }
////////////////////////////////


                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';
                mysql_select_db($dbname);
                $querystud = "SELECT * FROM enrollment e left join student s on e.studidno=s.studidno where codesecsemyear='" . $codesecsemyear . "' order by s.lname, s.fname";
                $allstud = mysql_query($querystud);
                $ctr = 0;

                $rowedit = mysql_fetch_array(mysql_query("SELECT MONTH(DATE_ADD(NOW(), INTERVAL 8 HOUR)) as mm, YEAR(DATE_ADD(NOW(), INTERVAL 8 HOUR)) as yyyy, DAY(DATE_ADD(NOW(), INTERVAL 8 HOUR)) as dd"));
                ?>


                <form class="cmxform" id="commentForm" method="post" action="AMMRecordAttendance.php">
                    <input name="Add" type="submit" value="Add"/><br>

                    <br><label for="btm">Enter Date of Class:</label>
                    <select id="btm" class="required" name="mm" style="margin-left: 4px;" tabindex="8">
                        <option value="">- Select Month -</option>
                        <option value="1" <?php
                if ($rowedit['mm'] == "1") {
                    echo "SELECTED";
                }
                ?> >January</option>
                        <option value="2" <?php
                                if ($rowedit['mm'] == "2") {
                                    echo "SELECTED";
                                } ?> >February</option>
                        <option value="3" <?php
                                if ($rowedit['mm'] == "3") {
                                    echo "SELECTED";
                                }
                ?> >March</option>
                        <option value="4" <?php
                                if ($rowedit['mm'] == "4") {
                                    echo "SELECTED";
                                } ?> >April</option>
                        <option value="5" <?php
                                if ($rowedit['mm'] == "5") {
                                    echo "SELECTED";
                                }
                ?> >May</option>
                        <option value="6" <?php
                                if ($rowedit['mm'] == "6") {
                                    echo "SELECTED";
                                } ?> >June</option>
                        <option value="7" <?php
                                if ($rowedit['mm'] == "7") {
                                    echo "SELECTED";
                                }
                ?> >July</option>
                        <option value="8" <?php
                                if ($rowedit['mm'] == "8") {
                                    echo "SELECTED";
                                } ?> >August</option>
                        <option value="9" <?php
                                if ($rowedit['mm'] == "9") {
                                    echo "SELECTED";
                                }
                ?> >September</option>
                        <option value="10" <?php
                                if ($rowedit['mm'] == "10") {
                                    echo "SELECTED";
                                } ?> >October</option>
                        <option value="11" <?php
                                if ($rowedit['mm'] == "11") {
                                    echo "SELECTED";
                                } ?> >November</option>
                        <option value="12" <?php
                                if ($rowedit['mm'] == "12") {
                                    echo "SELECTED";
                                }
                ?> >December</option>
                    </select>

                    <select id="btd" class="required" name="dd" style="margin-left: 4px;" tabindex="8">
                        <option value="">- Select Day -</option>
                        <option value="1" <?php
                                if ($rowedit['dd'] == "1") {
                                    echo "SELECTED";
                                }
                ?> >1</option>
                        <option value="2" <?php
                                if ($rowedit['dd'] == "2") {
                                    echo "SELECTED";
                                } ?> >2</option>
                        <option value="3" <?php
                                if ($rowedit['dd'] == "3") {
                                    echo "SELECTED";
                                }
                ?> >3</option>
                        <option value="4" <?php
                                if ($rowedit['dd'] == "4") {
                                    echo "SELECTED";
                                } ?> >4</option>
                        <option value="5" <?php
                                if ($rowedit['dd'] == "5") {
                                    echo "SELECTED";
                                }
                ?> >5</option>
                        <option value="6" <?php
                                if ($rowedit['dd'] == "6") {
                                    echo "SELECTED";
                                } ?> >6</option>
                        <option value="7" <?php
                                if ($rowedit['dd'] == "7") {
                                    echo "SELECTED";
                                }
                ?> >7</option>
                        <option value="8" <?php
                                if ($rowedit['dd'] == "8") {
                                    echo "SELECTED";
                                } ?> >8</option>
                        <option value="9" <?php
                                if ($rowedit['dd'] == "9") {
                                    echo "SELECTED";
                                }
                ?> >9</option>
                        <option value="10" <?php
                                if ($rowedit['dd'] == "10") {
                                    echo "SELECTED";
                                } ?> >10</option>
                        <option value="11" <?php
                                if ($rowedit['dd'] == "11") {
                                    echo "SELECTED";
                                }
                ?> >11</option>
                        <option value="12" <?php
                                if ($rowedit['dd'] == "12") {
                                    echo "SELECTED";
                                } ?> >12</option>
                        <option value="13" <?php
                                if ($rowedit['dd'] == "13") {
                                    echo "SELECTED";
                                }
                ?> >13</option>
                        <option value="14" <?php
                                if ($rowedit['dd'] == "14") {
                                    echo "SELECTED";
                                } ?> >14</option>
                        <option value="15" <?php
                                if ($rowedit['dd'] == "15") {
                                    echo "SELECTED";
                                }
                ?> >15</option>
                        <option value="16" <?php
                                if ($rowedit['dd'] == "16") {
                                    echo "SELECTED";
                                } ?> >16</option>
                        <option value="17" <?php
                                if ($rowedit['dd'] == "17") {
                                    echo "SELECTED";
                                }
                ?> >17</option>
                        <option value="18" <?php
                                if ($rowedit['dd'] == "18") {
                                    echo "SELECTED";
                                } ?> >18</option>
                        <option value="19" <?php
                                if ($rowedit['dd'] == "19") {
                                    echo "SELECTED";
                                } ?> >19</option>
                        <option value="20" <?php
                                if ($rowedit['dd'] == "20") {
                                    echo "SELECTED";
                                }
                ?> >20</option>
                        <option value="21" <?php
                                if ($rowedit['dd'] == "21") {
                                    echo "SELECTED";
                                } ?> >21</option>
                        <option value="22" <?php
                                if ($rowedit['dd'] == "22") {
                                    echo "SELECTED";
                                }
                ?> >22</option>
                        <option value="23" <?php
                                if ($rowedit['dd'] == "23") {
                                    echo "SELECTED";
                                } ?> >23</option>
                        <option value="24" <?php
                                if ($rowedit['dd'] == "24") {
                                    echo "SELECTED";
                                }
                ?> >24</option>
                        <option value="25" <?php
                                if ($rowedit['dd'] == "25") {
                                    echo "SELECTED";
                                } ?> >25</option>
                        <option value="26" <?php
                                if ($rowedit['dd'] == "26") {
                                    echo "SELECTED";
                                }
                ?> >26</option>
                        <option value="27" <?php
                                if ($rowedit['dd'] == "27") {
                                    echo "SELECTED";
                                } ?> >27</option>
                        <option value="28" <?php
                                if ($rowedit['dd'] == "28") {
                                    echo "SELECTED";
                                }
                ?> >28</option>
                        <option value="29" <?php
                                if ($rowedit['dd'] == "29") {
                                    echo "SELECTED";
                                } ?> >29</option>
                        <option value="30" <?php
                                if ($rowedit['dd'] == "30") {
                                    echo "SELECTED";
                                }
                ?> >30</option>
                        <option value="31" <?php
                                if ($rowedit['dd'] == "31") {
                                    echo "SELECTED";
                                } ?> >31</option>
                    </select>

                    <select id="bty" class="required" name="yyyy" style="margin-left: 4px;" tabindex="8">
                        <option value="">- Select Year -</option>
                        <option value="2011" <?php
                                if ($rowedit['yyyy'] == "2011") {
                                    echo "SELECTED";
                                }
                ?> >2011</option>
                        <option value="2010" <?php
                                if ($rowedit['yyyy'] == "2010") {
                                    echo "SELECTED";
                                }
                ?> >2010</option>
                        <option value="2009" <?php
                                if ($rowedit['yyyy'] == "2009") {
                                    echo "SELECTED";
                                }
                ?> >2009</option>
                        <option value="2008" <?php
                                if ($rowedit['yyyy'] == "2008") {
                                    echo "SELECTED";
                                }
                ?> >2008</option>
                        <option value="2007" <?php
                                if ($rowedit['yyyy'] == "2007") {
                                    echo "SELECTED";
                                } ?> >2007</option>
                        <option value="2006" <?php
                                if ($rowedit['yyyy'] == "2006") {
                                    echo "SELECTED";
                                }
                ?> >2006</option>
                        <option value="2005" <?php
                                if ($rowedit['yyyy'] == "2005") {
                                    echo "SELECTED";
                                }
                ?> >2005</option>
                        <option value="2004" <?php
                                if ($rowedit['yyyy'] == "2004") {
                                    echo "SELECTED";
                                }
                ?> >2004</option>
                        <option value="2003" <?php
                                if ($rowedit['yyyy'] == "2003") {
                                    echo "SELECTED";
                                }
                ?> >2003</option>
                        <option value="2002" <?php
                                if ($rowedit['yyyy'] == "2002") {
                                    echo "SELECTED";
                                }
                ?> >2002</option>
                        <option value="2001" <?php
                                if ($rowedit['yyyy'] == "2001") {
                                    echo "SELECTED";
                                }
                ?> >2001</option>
                        <option value="2000" <?php
                                if ($rowedit['yyyy'] == "2000") {
                                    echo "SELECTED";
                                }
                ?> >2000</option>
                        <option value="1999" <?php
                                if ($rowedit['yyyy'] == "1999") {
                                    echo "SELECTED";
                                }
                ?> >1999</option>
                        <option value="1998" <?php
                                if ($rowedit['yyyy'] == "1998") {
                                    echo "SELECTED";
                                }
                ?> >1998</option>
                        <option value="1997" <?php
                                if ($rowedit['yyyy'] == "1997") {
                                    echo "SELECTED";
                                }
                ?> >1997</option>
                        <option value="1996" <?php
                                if ($rowedit['yyyy'] == "1996") {
                                    echo "SELECTED";
                                }
                ?> >1996</option>
                        <option value="1995" <?php
                                if ($rowedit['yyyy'] == "1995") {
                                    echo "SELECTED";
                                }
                ?> >1995</option>
                        <option value="1994" <?php
                                if ($rowedit['yyyy'] == "1994") {
                                    echo "SELECTED";
                                }
                ?> >1994</option>
                        <option value="1993" <?php
                                if ($rowedit['yyyy'] == "1993") {
                                    echo "SELECTED";
                                }
                ?> >1993</option>
                        <option value="1992" <?php
                                if ($rowedit['yyyy'] == "1992") {
                                    echo "SELECTED";
                                }
                ?> >1992</option>
                        <option value="1991" <?php
                                if ($rowedit['yyyy'] == "1991") {
                                    echo "SELECTED";
                                }
                ?> >1991</option>
                        <option value="1990" <?php
                                if ($rowedit['yyyy'] == "1990") {
                                    echo "SELECTED";
                                }
                ?> >1990</option>
                    </select><br>
                    <table  border="0" id="myTable" class="tablesorter" style="width: 95%;" align="center">
                        <thead><tr>
                                <th style="width: 2.5%;">No</th>

                                <th style="width: 17%;">Name</th>
                                <th style="width: 2.5%;">Present</th>
                                <th style="width: 2.5%;">Late</th>
                                <th style="width: 2.5%;">Absent</th>

                            </tr></thead>
                        <tfoot><tr>
                                <th>No</th>
                                <th>Name</th>
                                <th>Present</th>
                                <th>Late</th>
                                <th>Absent</th>
                            </tr></tfoot>
                        <tbody>

                            <?php
                                //$row['lname'] . ",&nbsp;" . $row['fname'] . "&nbsp;" . $row['mname']
                                while ($row = mysql_fetch_array($allstud)) {
                                    $ctr++;
                                    //if()$row['studidno']
                                    //echo "<tr>" . "<td>" . $ctr . "</td>";<th>No.</th>
                                    echo "<tr><td  align=\"center\">" . $ctr . "</td>";
                                    echo "<td>" . $row['lname'] . ",&nbsp;" . $row['fname'] . "&nbsp;" . $row['mname'] . "</td>";
                                    echo "<td><input type=\"radio\" name=\"stat" . $ctr . "\" value=\"P\" checked/></td>";
                                    echo "<td><input type=\"radio\" name=\"stat" . $ctr . "\" value=\"L\" /></td>";
                                    echo "<td><input type=\"radio\" name=\"stat" . $ctr . "\" value=\"A\" /></td>";
                                    echo "<input type=\"text\" name=\"stud" . $ctr . "\" size=\"1\" value=\"" . $row['studidno'] . "\" style=\"visibility:hidden;\"/></tr>";
                                }
                                include 'DBLibrary/closeDB.php';
                            ?> </tbody>
                        </table>

                    </form>
                    <div id="pager" class="pager" align="center" style="float:left; height: auto; width: 100%;">
                        <form>
                            <img src="addons/pager/icons/first.png" class="first"/>
                            <img src="addons/pager/icons/prev.png" class="prev"/>
                            <input readonly="readonly" type="text" class="pagedisplay"/>
                            <img src="addons/pager/icons/next.png" class="next"/>
                            <img src="addons/pager/icons/last.png" class="last"/>
                            <select class="pagesize">
                                <option selected="selected"  value="10">10</option>

                                <option value="20">20</option>
                                <option value="30">30</option>
                                <option  value="40">40</option>
                            </select>
                        </form>
                        <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
                            <div align="center" style=" height: 4px; width: auto;"></div>
                            <bb class ="small">Copyright &#169; 2011</bb>
                        </div>
                    </div>
                    <br>


                </div>
            </div>

        </body>
    </html>
<?php
                            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                                echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
                            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
                                echo "<script language='javascript'>window.location='SMMViewStud.php'</script>";
                            }
?>